﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoreBDS;
using CoreProvince;

namespace BatDongSan.Manager
{
    public partial class ManProjectExecute : System.Web.UI.Page
    {
        public static int _projectId;
        public static bool iu = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _projectId = Convert.ToInt32(Request.QueryString["id"]);
                loadBusiness();
                loadField();
                loadProvince();
                loadWard();
                loadInfo();
            }
        }
        private void loadBusiness()
        {
            try
            {
                ddlBusiness.DataSource = BusinessBll.GetList(Utility.strConnect);
                ddlBusiness.DataTextField = "Name";
                ddlBusiness.DataValueField = "BusinessId";
                ddlBusiness.DataBind();
            }
            catch { }
        }
        private void loadField()
        {
            try
            {
                ddlField.DataSource = FieldOfActionBll.GetList(Utility.strConnect);
                ddlField.DataTextField = "Name";
                ddlField.DataValueField = "FieldId";
                ddlField.DataBind();
            }
            catch { }
        }
        private void loadProvince()
        {
            try
            {
                ddlCity.DataSource = ProvinceBll.GetList(Utility.strConnect);
                ddlCity.DataTextField = "Name";
                ddlCity.DataValueField = "Id";
                ddlCity.DataBind();
            }
            catch { }
        }
        private void loadWard()
        {
            try
            {
                ddlWard.DataSource = WardBll.GetList(false, true, Convert.ToInt32(ddlCity.SelectedValue), Utility.strConnect);
                ddlWard.DataTextField = "Name";
                ddlWard.DataValueField = "Id";
                ddlWard.DataBind();
            }
            catch { }
        }
        private void loadInfo()
        {
            try
            {
                Cvk_Project_Execute info = ProjectBll.claExecute.GetInfo(_projectId, Utility.strConnect);
                if (info != null)
                {
                    iu = true;
                    ddlBusiness.SelectedValue = Convert.ToString(info.BusinessId);
                    ddlField.SelectedValue = Convert.ToString(info.FieldId);
                    ddlCity.SelectedValue = Convert.ToString(info.CityId);
                    loadWard();
                    ddlWard.SelectedValue = Convert.ToString(info.DistrictId);
                }
            }
            catch { }
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadWard();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                Cvk_Project_Execute info = new Cvk_Project_Execute();
                info.ProjectId = _projectId;
                info.BusinessId = Convert.ToInt32(ddlBusiness.SelectedValue);
                info.FieldId = Convert.ToInt32(ddlField.SelectedValue);
                info.CityId = Convert.ToInt32(ddlCity.SelectedValue);
                info.DistrictId = Convert.ToInt32(ddlWard.SelectedValue);
                info.CreateDate = DateTime.Now;
                if (!iu)
                {
                    ProjectBll.claExecute.Insert(info, Utility.strConnect);
                    lbStatus.Text = "Gán thành công";
                }
                else { ProjectBll.claExecute.Update(info, Utility.strConnect); lbStatus.Text = "Gán lại thành công"; }
                loadInfo();
            }
            catch { }
        }
    }
}